//
//  StatusCell.swift
//  ydy_weibo
//
//  Created by 杨德勇 on 16/7/18.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

import UIKit
import SnapKit
//控制文本与边缘的间隙，还是很多控件之间的位置，请不要修改
let StatusCellMargin :CGFloat = 12
let StatusIconWidth : CGFloat = 35


class StatusCell: UITableViewCell {

    //记录底部约束 根据配图动态变化
    var bottomConstraints: Constraint?
    
    //微博数据模型
    var status : Status?{
        didSet{
            originalView.status = status
            
            self.bottomConstraints?.uninstall()
            
            if let re = status?.retweeted_status {
                retweetedView.retweetedStatus = re
                retweetedView.hidden = false
                
                bottomView.snp_updateConstraints(closure: { (make) in
                    self.bottomConstraints = make.bottom.equalTo(bottomView.snp_bottom).offset(StatusCellMargin).constraint
                })
            }else{
                //不是转发微博
                retweetedView.hidden = true
                bottomView.snp_updateConstraints(closure: { (make) in
                    self.bottomConstraints = make.bottom.equalTo(bottomView.snp_bottom).offset(StatusCellMargin).constraint
                })
            }
            
        }
    }
    
    //MARK:-
    //MARK:3.init
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .None
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    
    //MARK:-
    //MARK:2.setup UI
    private func setupUI(){
        contentView.addSubview(originalView)
        contentView.addSubview(retweetedView)
        contentView.addSubview(bottomView)
        
        
        
        originalView.snp_makeConstraints { (make) in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
        }
        
        retweetedView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.left.equalTo(originalView.snp_left)
            make.right.equalTo(originalView.snp_right)
        }
        
        bottomView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(40)
            
            self.bottomConstraints = make.top.equalTo(retweetedView.snp_bottom).constraint
        }
        
        self.snp_makeConstraints { (make) in
            make.bottom.equalTo(bottomView.snp_bottomMargin)
            make.top.left.right.equalTo(self)
        }
    }

    
    
    //MARK:-
    //MARK:1.懒加载所有控件
    private lazy var originalView :StatusOriginalView = StatusOriginalView()
    private lazy var retweetedView : ReweetedStatusView = ReweetedStatusView()
    private lazy var bottomView :StatusCellBottomview = StatusCellBottomview()

}
